from pydantic import BaseModel, Field
from typing import Optional, List
from datetime import datetime


class TaskBase(BaseModel):
    name: str = Field(..., description="任务名称")
    description: Optional[str] = Field(default=None, description="任务描述")
    engine_name: str = Field(..., description="使用的模糊测试引擎")
    target_id: int = Field(..., description="目标ID")
    config: str = Field(..., description="引擎配置（JSON格式）")


class TaskCreate(TaskBase):
    pass


class TaskUpdate(BaseModel):
    name: Optional[str] = None
    description: Optional[str] = None
    config: Optional[str] = None
    status: Optional[str] = None


class TaskInDB(TaskBase):
    id: int
    status: str
    progress: float
    celery_task_id: Optional[str] = None
    created_at: datetime
    started_at: Optional[datetime] = None
    completed_at: Optional[datetime] = None
    
    class Config:
        from_attributes = True


class TaskResultBase(BaseModel):
    result_type: str = Field(..., description="结果类型（崩溃、漏洞等）")
    result_data: str = Field(..., description="结果数据（JSON格式）")
    severity: Optional[str] = Field(default=None, description="严重性")


class TaskResultCreate(TaskResultBase):
    task_id: int = Field(..., description="任务ID")


class TaskResultInDB(TaskResultBase):
    id: int
    task_id: int
    found_at: datetime
    
    class Config:
        from_attributes = True


class ArtifactBase(BaseModel):
    artifact_type: str = Field(..., description="制品类型（日志、崩溃样本等）")
    file_path: str = Field(..., description="文件路径")
    s3_key: str = Field(..., description="MinIO中的key")
    size: int = Field(..., description="文件大小")


class ArtifactCreate(ArtifactBase):
    task_id: int = Field(..., description="任务ID")


class ArtifactInDB(ArtifactBase):
    id: int
    task_id: int
    created_at: datetime
    
    class Config:
        from_attributes = True